package yxy.game.pm2.command.union.leader;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.module.RedisLock;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.UnionLeaderMessage;
import yxy.game.pm2.module.union.UnionBean;
import yxy.game.pm2.module.union.UnionHandle;
import yxy.game.pm2.module.union.UnionHandle.UnionPlayerEntry;
import yxy.game.pm2.module.union.UnionName;

/** 联盟盟主 联盟信息修改 */
@MessageType(proto = Command.UnionInfoRemend, classtype = UnionLeaderMessage.UnionInfoRemendRequest.class)
public class CMD_UnionInfoRemend extends Command<UnionLeaderMessage.UnionInfoRemendRequest> {

	@Override
	public void execute(MessageSource messageSource, UnionLeaderMessage.UnionInfoRemendRequest request) {
		String playerCode = messageSource.getTag();
		String name = request.getName();
		String abbr = request.getAbbr();
		int flag = request.getFlag();

		UnionLeaderMessage.UnionInfoRemendResponse.Builder response = UnionLeaderMessage.UnionInfoRemendResponse.newBuilder();

		UnionHandle unionHandle = new UnionHandle();
		UnionName unionName = UnionName.getInstance();

		// 管理员权限
		UnionPlayerEntry entry = new UnionPlayerEntry(playerCode);
		Result result = unionHandle.checkPlayerUnionManager(entry);
		if (!result.equals(Result.UnionManager)) {
			send(messageSource, response, result);
			return;
		}
		RedisLock uLock = UnionBean.lock(entry.getUnionBean().getCode());
		try {
			if (uLock.tryLock() && unionName.tryLock()) {
				UnionBean union = entry.reloadUnionBean();

				// 检查名字
				// 字符整形 去掉空格 全角改半角
				name = UnionName.getInstance().checkName(name);
				abbr = UnionName.getInstance().checkName(abbr);
				if (name.equals(union.getName()) && abbr.equals(union.getAbbr()) && flag == union.getFlag()) {
					response.setName(union.getName());
					response.setAbbr(union.getAbbr());
					response.setFlag(union.getFlag());
					send(messageSource, response, Result.Success);
					return;
				}

				result = new UnionHandle().checkName(name, abbr, union.getName());
				if (!result.equals(Result.Success)) {
					send(messageSource, response, result);
					return;
				}

				// 改名
				union.setName(name);
				union.setAbbr(abbr);
				union.setFlag(flag);

				union.set();
				union.active();
				union.name();

				// 封消息
				response.setName(union.getName());
				response.setAbbr(union.getAbbr());
				response.setFlag(union.getFlag());

				send(messageSource, response, Result.Success);
			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			uLock.unlock();
			unionName.unlock();
		}
	}

	private void send(MessageSource messageSource, UnionLeaderMessage.UnionInfoRemendResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}